/**
 * Created by sens on 2020/5/3.
 */
/**
 * Created by sens on 2020/5/2.
 */
(function () {
    typeof window.sens !== 'object' && (window.sens = {});
})(window);

Object.assign(window.sens, (function () {
    let level = 1;
    let process = 1;
    let monsterName = ''

    let maxProp = 1000;
    let spdEff = 0.8;
    let aglEff = 0.8;
    let main;
    let battleData;
    let option;

    let log = sens.log;
    /**
     * 构造玩家
     * @param name
     * HP:血量
     * att:攻
     * def:防
     * spe:速
     * agl:敏
     * int:智
     * skill:技
     * luck:运
     */

    /**
     * 文字输出
     */

    function calcMiss(_my, _other, aglFix, aglEff) {
        if (_other.agl < _my.agl * aglFix) {
            return false;
        }

        let randMiss = Math.floor(Math.random() * 200);
        let miss = (_other.aglStore - _my.aglStore * aglFix < 0 ? 0 : _other.aglStore - _my.aglStore * aglFix);
        let isMiss = false;
        let addAgl = _other.agl - _my.agl * aglFix;

        if (randMiss * aglFix < miss) {
            isMiss = true;

            if (_other.agl > _my.agl) {
                _other.aglStore /= 2;
            }
        } else {
            if (_other.agl > _my.agl * aglFix && _other.agl < _my.agl) {
                _other.aglStore -= (_other.agl - _my.agl * aglFix);
            }

            _other.aglStore += addAgl * aglEff;
        }

        if (is1P) {
            console.info(_my.name + ':' + _my.aglStore + '  ' + _other.name + ':' + _other.aglStore);
        } else {
            console.info(_other.name + ':' + _other.aglStore + '  ' + _my.name + ':' + _my.aglStore);
        }

        return isMiss;
    }

    //主要故事流程
    function story(_my, _other, is1P) {
        _my.skillStore += Math.round((_my.skill + _my.int * 0.5));
        option.onSkillChange(is1P, _my.skillStore / maxProp * 2);
        if (Math.floor(Math.random() * maxProp / 2 + 50) <= _my.skillStore) {
            // 技能攻击

            let arrSkills = [];

            let skills = _my.skills;
            skills.forEach(function (obj) {
                let num = obj.rate;

                for (let i = 0; i < num; i++) {
                    arrSkills.push(obj);
                }
            });
            let skillNum = arrSkills.length;

            if (skillNum) {
                let skill = arrSkills[Math.round(Math.random() * (skillNum - 1))];

                if (_my.skillStore > skill.need) {
                    (skill.start)(_my, _other, is1P);
                    option.onBloodChange(is1P, _other.HP / _other.maxHp);
                    option.onSkillChange(is1P, _my.skillStore / maxProp * 2);
                } else {
                    att();
                }
            } else {
                att();
            }
        } else {
            att();
        }

        function att() {
            // 物理攻击
            let isMiss = calcMiss(_my, _other, 1, aglEff);

            if (!isMiss) {
                let hurt = Math.floor(_my.att * (maxProp - _other.def + _my.att * 0.5) / maxProp) + Math.floor(Math.random() * 10) + 1;

                _other.HP -= hurt;
                _other.HP <= 0 && (_other.HP = 0);
                option.onBloodChange(is1P, _other.HP > 0 ? _other.HP / _other.maxHp : 0);

                log('[' + _my.name + ']挥舞拳头对[' + _other.name + ']造成了【' + hurt + '】点伤害, [' + _other.name + ']剩余HP:' + _other.HP, is1P);

                _other.HP === 0 && sens.stop(_my, _other);
            } else {
                log('[' + _my.name + ']攻击[' + _other.name + '],但[' + _other.name + ']非常风骚的躲开了攻击,剩余HP:' + _other.HP, is1P);
            }
        }
    }

    //主进程
    let is1P = true;

    function mainstream() {
        return function () {
            function calcSpd() {
                battleData.p1.spd += battleData.p1.spe + maxProp / 10;
                battleData.p2.spd += battleData.p2.spe + maxProp / 10;

                if (battleData.p1.spd < maxProp && battleData.p2.spd < maxProp) {
                    calcSpd();
                } else if (battleData.p1.spd >= maxProp && battleData.p2.spd >= maxProp) {
                    if (battleData.p1.spd >= battleData.p2.spd) {
                        is1P = true;
                        resetSpd(battleData.p1);
                    } else {
                        is1P = false;
                        resetSpd(battleData.p2);
                    }
                } else if (battleData.p1.spd >= maxProp) {
                    is1P = true;
                    resetSpd(battleData.p1);
                } else if (battleData.p2.spd >= maxProp) {
                    is1P = false;
                    resetSpd(battleData.p2);
                } else {
                    console.error('spd code error')
                }
            }

            function resetSpd(player) {
                player.spd -= maxProp;
            }

            calcSpd();

            if (is1P) {
                story(battleData.p1, battleData.p2, true);
            } else {
                story(battleData.p2, battleData.p1);
            }
        }
    }

    function start(userOption) {
        option = userOption;
        battleData = option.battleData;

        main = setInterval(sens.mainstream(), 100);
    }

    function stop(_my, _other) {
        log(`[${_other.name}]倒在了[${_my.name}]的脚下`);

        clearInterval(main);
        option.onEnd(_my);
    }

    return {
        getOption: function () {
            return option;
        },
        level: level,
        process: process,
        monsterName: monsterName,
        start: start,
        stop: stop,
        mainstream: mainstream
    }
})());